import { createStoreBindings } from 'mobx-miniprogram-bindings'
import Tim from '../../model/tim'
import { timStore } from '../../store/tim'

// pages/conversation/conversation.js
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		targetUserId: null,
		service: null,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.storeBindings = createStoreBindings(this, {
			store: timStore,
			fields: ['sdkReady'],
			actions: ['pushMessage', 'resetMessage'],
		})
		// const targetUserId = options.targetUserId
		// const targetUserId =
		this.setData({
			// targetUserId: options.targetUserId,
			targetUserId: 'user1',
			service: options.service ? JSON.parse(options.service) : null,
		})
	},
	onunload() {
		this.resetMessage()
		this.storeBindings.destoryStoreBindings()
	},
	handleSendMessage(event) {
		console.log(event)
		const { type, content } = event.detail
		const message = Tim.getInstance().createMessage(
			type,
			content,
			this.data.targetUserId
		)
		this.pushMessage(message)
		Tim.getInstance().sendMessage(message)
		this.getOpenerEventChannel().emit('sendMessage')
	},
	handleLogin() {
		wx.navigateTo({
			url: '/pages/login/login',
		})
	},
})
